package foundation.fileUtil;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

public class ComputerUtil {

	/**
	 * 设置电脑10分钟后关机
	 */
	public static void ShutDown() {
		Runtime rt = Runtime.getRuntime();
		try {
			rt.exec("shutdown.exe -s -t 600");
			/*
			 * 40的单位为秒，可以改成你想要的任何数字。 如果是想定时关机，可用这句： rt.exec(
			 * "at 19：00 shutdown.exe -s");19：00可以换成你想要的时间
			 */
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除文件夹及文件夹里面所有内容
	 * @param folderPath
	 */
	public static void delFolder(String folderPath) {
		try {
			delAllFile(folderPath); // 删除完里面所有内容
			 String filePath = folderPath;
			 filePath = filePath.toString();
			 java.io.File myFilePath = new java.io.File(filePath);
			 myFilePath.delete(); //删除空文件夹
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void mkdir(){
		File file = new File("C:\\temp");
		if(!file.exists()){
		file.mkdir();
		}
	}

	public static boolean delAllFile(String path) {
		boolean flag = false;
		File file = new File(path);
		if (!file.exists()) {
			return flag;
		}
		if (!file.isDirectory()) {
			return flag;
		}
		String[] tempList = file.list();
		File temp = null;
		for (int i = 0; i < tempList.length; i++) {
			if (path.endsWith(File.separator)) {
				temp = new File(path + tempList[i]);
			} else {
				temp = new File(path + File.separator + tempList[i]);
			}
			if (temp.isFile()) {
				temp.delete();
			}
			if (temp.isDirectory()) {
				delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
				delFolder(path + "/" + tempList[i]);// 再删除空文件夹
				flag = true;
			}
		}
		return flag;
	}
	
	public void startNewThread(){
		TimerTask task = new TimerTask() {
			@Override
			public void run() {
				// task to run goes here
				ComputerUtil.delAllFile("E:/workspace/.metadata/.plugins/org.eclipse.core.resources/.history");
			}
		};
		Timer timer = new Timer();
		long delay = 0;
		long intevalPeriod = 3600 * 1000;
		// schedules the task to be run in an interval
		timer.scheduleAtFixedRate(task, delay, intevalPeriod);
	}

	public static void main(String[] args) {

		TimerTask task = new TimerTask() {
			@Override
			public void run() {
				// task to run goes here
				delFolder("E:/workspace/.metadata/.plugins/org.eclipse.core.resources/.history");	
				System.out.println("deleted");
			}
		};
		Timer timer = new Timer();
		long delay = 0;
		long intevalPeriod = 5 * 1000;
		// schedules the task to be run in an interval
		timer.scheduleAtFixedRate(task, delay, intevalPeriod);
		
		for(int i=0;i<5;i++){
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("第"+i+"次输出world");
		}
		timer.cancel();
		System.out.println("结束");


//		delFolder("E:/workspace/.metadata/.plugins/org.eclipse.core.resources/.history");
//		System.out.println("deleted");
	}

}
